﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using VitCode.Castle.DataAccess;
using VitCode.Castle.Entities;
using VitCode.Castle.Helper;
using VitCode.WorkFlow.DataAccess;
using VitCode.WorkFlow.Entities;

namespace WorkFlowSettingApp
{
    public partial class ActivityList : Form
    {
        public ActivityList()
        {
            InitializeComponent();
        }

        public string ListSql
        {
            get
            {
                return "select Id,Name as 名称, Memo as 备注, "
+ "replace(Convert(varchar(20),CreateTime,111),'/','-') as 创建日期, "
+ "replace(Convert(varchar(20),CloseTime,111),'/','-') as 停用日期, "
+ "(case isnull(IsUsed,0) when 1 then '是' else '否'  end) as  是否在用 "
+ "from workflow_activity";
            }
        }

        public string KeyProName
        {
            get { return "Id"; }
        }

        public string QueryCondition { get; set; }

        private void SetControls()
        {
            SqlHelper sqlHelper = new SqlHelper();
            WorkFlowAccess acc = new WorkFlowAccess();
            try
            {
                DataTable dt = sqlHelper.ExecuteDataSet(acc.Session, ListSql + " Where 1=2 ").Tables[0];
                if (dt != null)
                {
                    dgvList.DataSource = dt;
                    foreach (DataGridViewColumn col in dgvList.Columns)
                    {
                        if (col.Name == KeyProName)
                        {
                            col.Visible = false;
                            continue;
                        }
                        cboCondition.Items.Add(col.Name);
                    }
                }
            }
            finally
            {
                acc.CloseSession();
            }
            if (cboCondition.Items.Count > 0)
                cboCondition.SelectedIndex = 0;
        }

        private void dgvList_RowDividerDoubleClick(object sender, DataGridViewRowDividerDoubleClickEventArgs e)
        {

        }

        private void btnQuery_Click(object sender, EventArgs e)
        {

            if (txtVal.Text.Length > 0)
                QueryCondition = " Where " + cboCondition.SelectedItem + " like '%" + txtVal.Text + "%' ";
            string sql = ListSql + QueryCondition;
            SqlHelper sqlHelper = new SqlHelper();
            WorkFlowAccess acc = new WorkFlowAccess();
            try
            {
                DataTable dt = sqlHelper.ExecuteDataSet(acc.Session, sql).Tables[0];
                dgvList.DataSource = dt;
            }
            finally
            {
                acc.CloseSession();
            }
        }

        private void SelObject_Load(object sender, EventArgs e)
        {
            SetControls();
        }

        private void dgvList_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0)
            {
                DataGridViewRow dr = dgvList.Rows[e.RowIndex];
                var cellVal = dr.Cells[KeyProName].Value;
                if(cellVal != null)
                {
                    new ActivityCreator{KeyId=((Guid)cellVal)}.ShowDialog();
                }
                
            }
            //Close();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            new ActivityCreator { KeyId = (Guid.Empty) }.ShowDialog();
        }
    }
}
